package com.booksystem.campuslibrarymanagementsystem.controller.user;


import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.BookSearchrequest.BookselectR;
import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.Personalrequest.*;
import com.booksystem.campuslibrarymanagementsystem.commen.result.Result;
import com.booksystem.campuslibrarymanagementsystem.entity.*;
import com.booksystem.campuslibrarymanagementsystem.service.AccountTableService;
import com.booksystem.campuslibrarymanagementsystem.service.BooksTableService;
import com.booksystem.campuslibrarymanagementsystem.service.CategoriesTableService;
import com.booksystem.campuslibrarymanagementsystem.service.FavoritesTableService;
import com.booksystem.campuslibrarymanagementsystem.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/User")
@Api(tags = "个人收藏")
@RestController
public class PersonalCollectionController {

    FavoritesTable favoritesTable = new FavoritesTable();
    @Autowired
    BooksTableService booksTableService;
    @Autowired
    FavoritesTableService favoritesTableService;
    CategoriesTableService categoriesTableService;
    AccountTableService accountTableService;

    public PersonalCollectionController(FavoritesTableService favoritesTableService) {
        this.favoritesTableService = favoritesTableService;
    }

    /*
     添加收藏
        1，输入你的账户id,和图书id
        2，首先检查该用户是否已经收藏了这本书,判断是否重复
        3,根据图书id,将图书名称，作者，类别，从图书表里添加到收藏表
     */
    @PostMapping("/personalCollection")
    @ApiOperation(value = "添加收藏列表", notes = "收藏自己喜欢的图书")
    public Result PersonalCollection(@RequestBody Favorites favorites) {
        Map<String, Object> parsed = JwtUtil.tokenPre(favorites.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            Object account = parsed.get("account");
            if (identity.equals("user")) {
                int bookid = favorites.getBookid();  //图书id
                FavoritesTable hh = favoritesTableService.existsByAccountAndBookid((String) account, bookid);
                // 首先检查该用户是否已经收藏了这本书
                if (hh != null) {
                    System.out.println(hh);
                    return Result.fail(String.format("您已经收藏过这本书：%s", bookid));
                }
                BooksTable in = booksTableService.add(bookid);
                if (in != null) {
                    Favoritesdvsd ff = new Favoritesdvsd();
                    ff.setAccount((String) account);
                    ff.setBookid(bookid);
                    ff.setTitle(in.getTitle());
                    ff.setAuthor(in.getAuthor());
                    ff.setCategory(in.getCategory());
                    int a = favoritesTableService.saved(ff);
                    System.out.println(a);
                    if (a > 0) {
                        return Result.success(ff, 1);
                    } else {
                        return Result.fail((String.format("该账户收藏失败：%s", (String) account)));
                    }
                } else {
                    return Result.fail("不存在");
                }
            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }

    }

    @PostMapping("/selectPersonalCollection")
    @ApiOperation(value = "收藏搜索", notes = "按照图书名称，图书类别，作者，进行搜索个人收藏功能")
    public Result<List<Favoritesdvsd>> selectCollection(@RequestBody SelectFavoriteslist selectFavoriteslist) {
        Map<String, Object> parsed = JwtUtil.tokenPre(selectFavoriteslist.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            Object account = parsed.get("account");
            if (identity.equals("user")) {
                //String Account = selectFavoriteslist.getAccount();
                List<FavoritesTable> accountTable = favoritesTableService.selectByaccount((String) account);
                if (accountTable != null) {
                    if (selectFavoriteslist.getCategory().isEmpty()) {
                        List<FavoritesTable> list = favoritesTableService.selectfavoritesList(selectFavoriteslist.getTitle(), selectFavoriteslist.getAuthor(), selectFavoriteslist.getCategory());
                        if (!list.isEmpty()) {
                            ArrayList<Favoritesdvsd> list1 = new ArrayList<>();
                            for (int i = 0; i < list.size(); i++) {
                                favoritesTable = list.get(i);
                                Favoritesdvsd bookselectR = new Favoritesdvsd();
                                bookselectR.setAccount(favoritesTable.getAccount());
                                bookselectR.setBookid(favoritesTable.getBookid());
                                bookselectR.setTitle(favoritesTable.getTitle());
                                bookselectR.setAuthor(favoritesTable.getAuthor());
                                bookselectR.setCategory(favoritesTable.getCategory());
                                list1.add(bookselectR);
                            }
                            return Result.success(list1, list1.size());
                        } else {
                            return Result.fail("没有找到符合的图书信息");
                        }
                    } else {
                        CategoriesTable categoriesTable = categoriesTableService.queryByCategoryName(selectFavoriteslist.getCategory());
                        if (categoriesTable != null) {
                            List<FavoritesTable> list = favoritesTableService.selectfavoritesList(selectFavoriteslist.getTitle(), selectFavoriteslist.getAuthor(), selectFavoriteslist.getCategory());
                            if (!list.isEmpty()) {
                                ArrayList<Favoritesdvsd> list1 = new ArrayList<>();
                                for (int i = 0; i < list.size(); i++) {
                                    favoritesTable = list.get(i);
                                    Favoritesdvsd bookselectR = new Favoritesdvsd();
                                    bookselectR.setBookid(favoritesTable.getBookid());
                                    bookselectR.setTitle(favoritesTable.getTitle());
                                    bookselectR.setAuthor(favoritesTable.getAuthor());
                                    bookselectR.setCategory(favoritesTable.getCategory());
                                    list1.add(bookselectR);
                                }
                                return Result.success(list1, list1.size());
                            } else {
                                return Result.fail("没有找到符合的图书信息");
                            }
                        } else {
                            return Result.fail("没有找到基于图书类别");
                        }
                    }

                } else {
                    return Result.fail("该学号不存在");
                }
            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }


    @PostMapping("/lookPersonalCollection")
    @ApiOperation(value = "查看所有收藏列表", notes = "账户id，进行查看自己的所有收藏列表")
    public Result<List<FavoritesTable>> lookCollection(@RequestBody Favoriteslist favoriteslist) {
        Map<String, Object> parsed = JwtUtil.tokenPre(favoriteslist.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            Object account = parsed.get("account");
            if (identity.equals("user")) {
                List<FavoritesTable> aa = favoritesTableService.selectByaccount((String) account);
                if (aa.isEmpty()) {
                    // 提供更具体的错误信息，说明是基于哪几个条件没有找到匹配的图书
                    return Result.fail(String.format("没有找到基于账户id：%s", (String) account));
                } else {
                    return Result.success(aa, 1);
                }
            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }

    @PostMapping("/deletePersonalCollection")
    @ApiOperation(value = "删除收藏书籍", notes = "账户id，收藏id进行删除该账户下该图书的收藏记录")
    public Result deleteCollection(@RequestBody DeleteFavoriteslist deleteFavoriteslist) {
        Map<String, Object> parsed = JwtUtil.tokenPre(deleteFavoriteslist.getToken());
        if (parsed != null) {
            Object identity = parsed.get("identity");
            Object account = parsed.get("account");
            if (identity.equals("user")) {
                int bookid = deleteFavoriteslist.getFavoriteid();  //收藏id
                int hh = favoritesTableService.deleteByAccountAndBookid((String) account, bookid);
                // 首先检查该用户是否已经收藏了这本书
                if (hh > 0) {
                    return Result.success("成功删除该图书收藏记录", 1);
                }
                return Result.fail(String.format("没有该图书的收藏记录：%s", bookid));
            } else {
                return Result.fail("身份权限不足");
            }
        } else {
            return Result.fail("请输入正确token值");
        }
    }


}

